package com.htthorizon.gateway.filter;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.htthorizon.common.core.constant.CacheConstants;
import com.htthorizon.common.core.context.SecurityContextHolder;
import com.htthorizon.common.core.utils.JwtUtils;
import com.htthorizon.common.core.utils.StringUtils;
import com.htthorizon.common.core.utils.ip.IpUtils;
import com.htthorizon.common.redis.service.RedisService;
import com.htthorizon.system.api.domain.SysLogininfor;
import com.htthorizon.system.api.domain.SysOperLog;
import com.htthorizon.system.api.exception.UnAllowAccessTimeException;
import com.netflix.ribbon.proxy.annotation.Http;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import javax.servlet.http.HttpServlet;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author Brilliant
 */
@Component
public class AllowAccessTimeFilter implements GlobalFilter, Ordered {

    @Autowired
    private RedisService redisService;

//    @Autowired
//    private RemoteLogService remoteLogService;


    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpRequest.Builder mutate = request.mutate();
        String userName = SecurityContextHolder.getUserName();
        String first = request.getHeaders().getFirst("x-Forwarded-For");
        String url1 = request.getURI().getPath();
        if (!url1.contains("login")) {
            return chain.filter(exchange.mutate().request(mutate.build()).build());
        }
        Date startTime = redisService.getCacheObject(CacheConstants.START_TIME_KEY);
        Date endTime = redisService.getCacheObject(CacheConstants.END_TIME_KEY);
        if (Objects.isNull(startTime)||Objects.isNull(endTime)){
            return chain.filter(exchange.mutate().request(mutate.build()).build());
        }


        if (StringUtils.isEmpty(userName)&&"cheny393".equals(userName)) {
            return chain.filter(exchange.mutate().request(mutate.build()).build());
        }

        DateTime date = DateUtil.date();
        String format = DateUtil.format(date, "HH:mm:ss");
        DateTime parse = DateUtil.parse(format);
        String endTime1 = DateUtil.format(endTime, "HH:mm:ss");
        DateTime endTime2 = DateUtil.parse(endTime1);
        String startTime1 = DateUtil.format(startTime, "HH:mm:ss");
        DateTime startTime2 = DateUtil.parse(startTime1);
        boolean before = parse.isBefore(endTime2);
        boolean after = parse.isAfter(startTime2);


        if (before && after) {
            return chain.filter(exchange.mutate().request(mutate.build()).build());
        }
//        SysOperLog operLog = new SysOperLog();
//        operLog.setLevel(3);
//        operLog.setType("系统级");
//        operLog.setStatus(1);
//        operLog.setErrorMsg("用户未在访问时间段内访问");
//
//        // 设置请求方式
//        operLog.setRequestMethod(ServletUtils.getRequest().getMethod());
//
//        // 保存数据库
//
//        try {
//            remoteLogService.saveLog(operLog, SecurityConstants.INNER);
//        } catch (Exception e) {
//
//        }
        HttpHeaders headers1 = request.getHeaders();
        List<String> list1 = headers1.get("authorization");
        String userName1=null;
        if (list1!=null&&!(list1.isEmpty())){
            String token = list1.get(0);
            userName1 = JwtUtils.getUserName(token);
        }
        SysOperLog sysLogininfor = SysOperLog.build(userName1, IpUtils.getIpAddr1( exchange.getRequest()), "未在指定访问时间段访问",request.getMethod().toString(),url1);
        redisService.setCacheObject("log:"+sysLogininfor.getCreateTime(),sysLogininfor);
        throw new UnAllowAccessTimeException();
    }

    @Override
    public int getOrder() {
        return -12344;
    }

}
